package com.example.tvapp.myandserver.mycontroller;

import com.example.tvapp.URL_REGULARS.URL_RULES;
import com.yanzhenjie.andserver.annotation.*;
import com.yanzhenjie.andserver.http.HttpRequest;

import java.util.*;

//包名小写字母，类名驼峰格式，不然可能出错
//严格使用@RequestMappinghe@PostMapping不然传递数据会失败
//前端ajax请求的type要求严格，与上一条的注解对应
//直接请求，后台使用@GetMapping
@Controller
public class MyController
{


    @GetMapping(path ="/index")
    public String main() throws Exception
    {

        return "/templates/index";
    }

    @GetMapping(path ="/getWebUrl")
    @ResponseBody
    public String getWebUrl() throws Exception
    {
        ArrayList<String[]> videoWebUrl=URL_RULES.getURLS();
        String result="";
        for(int i=videoWebUrl.size()-1;i>=0;i--)
        {
           // System.out.println("哈哈哈"+videoWebUrl.get(i)[0]);
            result=result+videoWebUrl.get(i)[0]+"__"+videoWebUrl.get(i)[videoWebUrl.get(i).length-1];//第一个是url,最后一个编号，中间的是其他信息
            if(i!=0)
                result=result+"\n";
        }
      //  System.out.println("请求"+result);
        return result;
    }
    @GetMapping(path ="/getCollect")
    @ResponseBody
    public String getCollect() throws Exception
    {
        ArrayList<String[]> collects=URL_RULES.getCOLLECTS();
        String result="";
        for(int i=collects.size()-1;i>=0;i--)
        {
            String[] c=collects.get(i);
            for(int j=0;j<c.length;j++)
                result=result+c[j]+"__";
            if(i!=0)
             result=result+"\n";
        }
       // System.out.println("返回"+result);
        return result;
    }
    @PostMapping(path = "/addRule")
    @ResponseBody
    public String addRule(HttpRequest request) throws Exception
    {
        String rule=request.getParameter("rule");
        System.out.println(rule);
        URL_RULES.addRULES(rule,null);
        return "成功";
    }
    @PostMapping(path = "/addUrl")
    @ResponseBody
    public String addUrl(HttpRequest request) throws Exception
    {
        String url=request.getParameter("url");
        System.out.println(url);
        URL_RULES.addURLS(url,null);
        return "成功";
    }
    @PostMapping(path = "/deleteCollect")
    @ResponseBody
    public String deleteCollect(HttpRequest request) throws Exception
    {
        String row=request.getParameter("row");
        System.out.println(row);
        URL_RULES.deleteCOLLECTS(Integer.parseInt(row));
        return "成功";
    }
    @PostMapping(path = "/deleteWebUrl")
    @ResponseBody
    public String deleteWebUrl(HttpRequest request) throws Exception
    {
        String row=request.getParameter("row");
        URL_RULES.deleteURLS(Integer.parseInt(row));
        return "成功";
    }


}
